package com.android.nurse;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/**
 * Class common for get configuration information
 * @author NamNT
 *
 */
public class CommonActivity extends Activity {
	private static String strConfigJson = null;
	
	public void setStrConfigJson() { 
		if (getStrConfigJson() == null) {
			CommonActivity.strConfigJson = readFile(getApplicationContext(), "Data/configs.json");
			
		} else {
			CommonActivity.strConfigJson = getStrConfigJson();
		}
	}

	public String getStrConfigJson() {
		return strConfigJson;
	}
	
	/**
	 * Read file json from assets/Data
	 * @param context: Context of Activity
	 * @param filename: path of file 
	 * @return a string save content of file
	 */ 
	public String readFile(Context context, String filename) {
		InputStream in = null;
		OutputStream out = null;
		try {
			in = context.getAssets().open(filename);
			out = new ByteArrayOutputStream();
			int i = in.read();
			while (i != -1) {
				out.write(i); 
				i = in.read();
			}
			in.close();
		} catch (IOException e) {
			//return ""
			return "";
		}
		return out.toString();
	}
	
	/**
	 * get tired coefficient from config file, default value is 1.5
	 * @return tired coefficient value
	 */
	public double getTiredCoefficient() {
		double value = 1.5;		
		try {
			setStrConfigJson();				
			JSONObject jObject = new JSONObject(getStrConfigJson());
			JSONArray jsonArray = jObject.getJSONArray("logic");
			value = jsonArray.getJSONObject(0).getDouble("tired_coefficient");
		} catch (Exception e){
			//return default value.			
		}
		return value;
	}
	
	/**
	 * get nurse type obj from config file
	 * @param id int
	 * @return NurseType
	 */
	public NurseType getNurseType(int id) {
		NurseType nurseType = new NurseType();
		try {
			setStrConfigJson();				
			JSONObject jObject = new JSONObject(getStrConfigJson());
			JSONArray jsonArray = jObject.getJSONArray("type_master");
			JSONObject jsonObj;
			for (int i = 0; i < jsonArray.length(); i++) {
				jsonObj = jsonArray.getJSONObject(i);
				if ((jsonObj.getInt("id") == id)) {
					nurseType.setId(id);
					nurseType.setName(jsonObj.getString("name"));
					nurseType.setImage(jsonObj.getString("image"));
					nurseType.setImageS(jsonObj.getString("image_s"));
					nurseType.setImageRanking(jsonObj.getString("image_ranking"));
					nurseType.setDescription(jsonObj.getString("description"));
					break;
				}
			}
			
			
		} catch (Exception e){
			//return default value.			
		}
		return nurseType;
	}
	/**
	 * Get show ranking flg: return true -> show all, false show local ranking. 
	 * @return boolean
	 */
	public boolean getShowRanking() {
		boolean show_flg = false;
		try {
			setStrConfigJson();				
			JSONObject jObject = new JSONObject(getStrConfigJson());
			JSONArray jsonArray = jObject.getJSONArray("show_ranking_all");
			JSONObject jsonObj = jsonArray.getJSONObject(0);
			show_flg = jsonObj.getBoolean("show_flag");	
			Log.d("B", "B: "+ show_flg);
		} catch (Exception e){
			//return default value.
			e.printStackTrace();
			Log.d("Ex", "Opps " + e.getStackTrace());
		}
		return show_flg;
	}
	
	/**
	 * Get Tired Description
	 * @param id
	 * @return
	 */
	public TiredDescription getTiredDescription(int id) {
		TiredDescription tiredDescription = new TiredDescription();
		try {
			setStrConfigJson();				
			JSONObject jObject = new JSONObject(getStrConfigJson());
			JSONArray jsonArray = jObject.getJSONArray("tired_description");
			JSONObject jsonObj;
			for (int i = 0; i < jsonArray.length(); i++) {
				jsonObj = jsonArray.getJSONObject(i);
				if ((jsonObj.getInt("id") == id)) {
					tiredDescription.setId(id);
					tiredDescription.setName(jsonObj.getString("name"));					
					tiredDescription.setDescription(jsonObj.getString("description"));
					break;
				}
			}
			
		} catch (Exception e){
			//return default value.			
		}
		return tiredDescription;
	}
	
	/**
	 * get list age for spinner
	 * @return
	 */
	public ArrayList<AgeMaster> getAgeMaster() {
		ArrayList<AgeMaster> lstAge = new ArrayList<AgeMaster>();
		AgeMaster age = new AgeMaster();
		age.setKey(0);
		age.setValue("");
		lstAge.add(age);
		AgeMaster ageMaster;
		try {
			setStrConfigJson();				
			JSONObject jObject = new JSONObject(getStrConfigJson());
			JSONArray jsonArray = jObject.getJSONArray("age_pulldown_master");
			JSONObject jsonObj;
			for (int i = 0; i < jsonArray.length(); i++) {
				jsonObj = jsonArray.getJSONObject(i);
				ageMaster = new AgeMaster();
				ageMaster.setValue(jsonObj.getString(""+((i + 1) * 10)));
				ageMaster.setKey(((i + 1) * 10));
				lstAge.add(ageMaster);			
			}
			
		} catch (Exception e){
			//return default value.			
		}
		return lstAge;
	}
	
	/**
	 * Get default title when there no results
	 * @return
	 */
	public String getDefaultTitle() {
		String title = "";
		try {
			setStrConfigJson();				
			JSONObject jObject = new JSONObject(getStrConfigJson());
			JSONArray jsonArray = jObject.getJSONArray("default");
			title = jsonArray.getJSONObject(0).getString("njb_intro_title");
		} catch (Exception e){
			//return default value.			
		}
		return title;
	}
	
	/**
	 * Get default body when there no results
	 * @return
	 */
	public String getDefaultBody() {
		String body = "";
		try {
			setStrConfigJson();				
			JSONObject jObject = new JSONObject(getStrConfigJson());
			JSONArray jsonArray = jObject.getJSONArray("default");
			body = jsonArray.getJSONObject(1).getString("njb_intro_body");
		} catch (Exception e){
			//return default value.			
		}
		return body;
	}
	
	/**
	 * Get Get Url for get server ranking
	 * @return get url String
	 */
	public String getGetURL() {
		String url = "";
		try {
			setStrConfigJson();				
			JSONObject jObject = new JSONObject(getStrConfigJson());
			JSONArray jsonArray = jObject.getJSONArray("api_ranking");
			url = jsonArray.getJSONObject(0).getString("get_url");
		} catch (Exception e){
			//return default value.			
		}
		return url;
	}
	
	/**
	 * Get Post Url for post ranking to server
	 * @return post url String
	 */
	public String getPostURL() {
		String url = "";
		try {
			setStrConfigJson();				
			JSONObject jObject = new JSONObject(getStrConfigJson());
			JSONArray jsonArray = jObject.getJSONArray("api_ranking");
			url = jsonArray.getJSONObject(1).getString("post_url");
		} catch (Exception e){
			//return default value.			
		}
		return url;
	}
	
	/**
	 * get YES value when answer question
	 * @return
	 */
	public int getYesAnswer() {
		int answer = 0;
		try {
			setStrConfigJson();				
			JSONObject jObject = new JSONObject(getStrConfigJson());
			JSONArray jsonArray = jObject.getJSONArray("question_judg_master");
			answer = jsonArray.getJSONObject(0).getInt(Constant.ANSWER_YES);
		} catch (Exception e){
			//return default value.			
		}
		return answer;
	}
	
	/**
	 * get NO value when answer question
	 * @return
	 */
	public int getNoAnswer() {
		int answer = 1;
		try {
			setStrConfigJson();				
			JSONObject jObject = new JSONObject(getStrConfigJson());
			JSONArray jsonArray = jObject.getJSONArray("question_judg_master");
			answer = jsonArray.getJSONObject(1).getInt(Constant.ANSWER_NO);
		} catch (Exception e){
			//return default value.			
		}
		return answer;
	}
	
	public void customeActivity(Intent i) {
		startActivity(i);
		overridePendingTransition(R.anim.come_on, R.anim.leave);
	}
	public void backScreen(Intent i){
		startActivity(i);
		overridePendingTransition(R.anim.animation_enter, R.anim.animation_leave);
	}
	
}
